package cn.edu.sdjzu.cs.dingcan.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import cn.edu.sdjzu.cs.dingcan.entity.FoodType;
import cn.edu.sdjzu.cs.dingcan.service.FoodTypeService;

@WebServlet("/food-type")
public class FoodTypeServlet extends HttpServlet {

	/**
	 *
	 */
	private static final long serialVersionUID = 718534183870474636L;

	// 创建service
	private FoodTypeService service = new FoodTypeService();

	private Object uri;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		List<FoodType> list = service.findAll();
		config.getServletContext().setAttribute("foodtype", list);
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String method = request.getParameter("method");

		if ("add".equals(method)) {
			add(request, response);
		} else if ("list".equals(method)) {
			list(request, response);
		} else if ("update".equals(method)) {
			update(request, response);
		} else if ("delete".equals(method)) {
			delete(request, response);
		} else if ("search".equals(method)) {
			search(request, response);
		} else if ("show".equals(method)) {
			show(request, response);
		}

	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String name = request.getParameter("name");
			FoodType foodtype = new FoodType();
			foodtype.setTypeName(name);
			service.addNewType(foodtype);
			list(request, response);
		} catch (Exception e) {
			e.printStackTrace();			
		}
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<FoodType> list1 = service.findAll();
		request.setAttribute("list", list1);
		request.getServletContext().setAttribute("foodtype", list1);
		request.getRequestDispatcher("/sys/foodtype/cuisineList.jsp").forward(request, response);
		;
	}

	// 在菜系更新中显示菜系对应的类型的名称
	private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Long id = Long.parseLong(request.getParameter("id"));
		FoodType type;
		try {
			type = service.findById(id);
			request.setAttribute("type", type);
			request.getRequestDispatcher("/sys/foodtype/updateCuisine.jsp").forward(request, response);
		} catch (SQLException e) {
			throw new RuntimeException("执行数据库操作时遇到错误", e);
		}

	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		FoodType type = new FoodType();
		Map<String, String[]> map = request.getParameterMap();
		try {
			BeanUtils.populate(type, map);
		} catch (IllegalAccessException | InvocationTargetException e) {
			throw new RuntimeException(e);
		}
		service.updata(type);
		list(request, response);		
	}
	
	
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		Long id = Long.parseLong(request.getParameter("id"));
		service.delete(id);
		list(request, response);
	}
	
	
	
	private void search(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String keyword = request.getParameter("keyword");
		if(keyword != null){
			List<FoodType> list;
			try {
				list = service.query(keyword);
				request.setAttribute("list", list);
				request.getRequestDispatcher("/sys/foodtype/cuisineList.jsp").forward(request, response);	
			} catch (SQLException e) {
				throw new RuntimeException("执行数据库操作时遇到错误", e);
			}
					
		}
	}
		
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	   this.doGet(request, response);		
	}

}
